const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')

}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDates(days, todate) {
  var dateArry = [];
  for (var i = 0; i < days; i++) {
    var dateObj = dateLater(todate, i);
    dateArry.push(dateObj)
  }
  return dateArry;
}

function dateLater(dates, later) {
  let dateObj = {};
  let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  let date = new Date(dates);
  date.setDate(date.getDate() + later);
  let day = date.getDay();
  let yearDate = date.getFullYear();
  let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  dateObj.time = yearDate + '-' + month + '-' + dayFormate;
  dateObj.week = show_day[day];
  return dateObj;
}

const throttle = function(fn, interval) {
  var enterTime = 0; //触发的时间
  var gapTime = interval || 300; //间隔时间，如果interval不传，则默认300ms
  return function() {
    var context = this;
    var backTime = new Date(); //第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments);
      enterTime = backTime; //赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}

/*函数防抖*/
const debounce = function(fn, interval) {
  var timer;
  var gapTime = interval || 300; //间隔时间，如果interval不传，则默认1000ms
  return function() {
    clearTimeout(timer);
    var context = this;
    var args = arguments; //保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context, args);
    }, gapTime);
  };
}

var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t;
  } : function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
  },
  a = function(t) {
    return (t = t.toString())[1] ? t : "0" + t;
  },
  i = function(t) {
    console.log(t);
  },
  s = function(t) {
    return null == t || "" == t || "" == t.trim();
  },
  l = function(t) {
    wx.showToast({
      title: t,
      icon: "none"
    });
  },
  c = function(t, e, n) {
    return t + "?x-oss-process=image/resize,m_fill,h_" + n + ",w_" + e;
  },
  r = function(t) {
    return t + "?x-oss-process=video/snapshot,t_100,f_jpg,w_0,h_0,ar_auto";
  };
 const delay= function(e) {
    return new Promise(function(t) {
        return setTimeout(t, e);
    });
};

const setDataPromise=function(e) {
  var t = this;
  return new Promise(function(r) {
      return t.setData(e, r);
  });
}


module.exports = {
  formatTime: formatTime,
  debounce: debounce,
  throttle: throttle,
  formatDate: formatDate,
  getDates: getDates,
  delay:delay,
  log: i,
  isEmpty: s,
  showToast: l,
  setDataPromise:setDataPromise,
  showModal: function(t) {
    wx.showModal({
      title: "提示",
      content: t,
      showCancel: !1,
      success: function(t) {}
    });
  },
  checkPermission: function(t, e, n) {
    wx.getSetting({
      success: function(a) {
        i(a), null == a.authSetting[t] ? wx.authorize({
          scope: t,
          success: function() {
            i("checkPermission => success"), n();
          },
          fail: function() {
            i("checkPermission => fail");
          }
        }) : a.authSetting[t] ? n() : (i("checkPermission => 提示需要授权"), wx.showModal({
          title: "提示",
          content: e,
          success: function(t) {
            t.confirm ? (i("checkPermission => 用户点击确定"), wx.openSetting({})) : t.cancel && i("checkPermission => 用户点击取消");
          }
        }));
      }
    });
  }
}